import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _class;

/**
 * VisitPermission - 安全组访问权限tab
 * @date: 2019-11-22

 */
import React, { Component } from 'react';
import { Table, Button, Tag } from 'antd';
import { Bind } from 'lodash-decorators';
import { tableScrollWidth } from 'utils/utils';
import { Button as ButtonPermission } from 'components/Permission';
import { operatorRender, yesOrNoRender } from 'utils/renderer';
import intl from 'utils/intl';
var VisitPermissionTab = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), (_class = /*#__PURE__*/function (_Component) {
  _inherits(VisitPermissionTab, _Component);

  var _super = _createSuper(VisitPermissionTab);

  function VisitPermissionTab(props) {
    var _this;

    _classCallCheck(this, VisitPermissionTab);

    _this = _super.call(this, props);
    _this.defaultExpandedRowKeys = [];
    _this.state = {
      expandedRowKeys: []
    };
    return _this;
  }
  /**
   * expandAll - 全部展开
   */


  _createClass(VisitPermissionTab, [{
    key: "expandAll",
    value: function expandAll() {
      var defaultExpandedRowKeys = this.props.defaultExpandedRowKeys;
      this.setState({
        expandedRowKeys: defaultExpandedRowKeys
      });
    }
    /**
     * 全部收起
     */

  }, {
    key: "collapseAll",
    value: function collapseAll() {
      this.setState({
        expandedRowKeys: []
      });
    }
    /**
     * 展开树
     * @param {boolean} expanded - 是否展开
     * @param {record} record - 当前行数据
     */

  }, {
    key: "onExpand",
    value: function onExpand(expanded, record) {
      var _this$state$expandedR = this.state.expandedRowKeys,
          expandedRowKeys = _this$state$expandedR === void 0 ? [] : _this$state$expandedR;
      this.setState({
        expandedRowKeys: expanded ? expandedRowKeys.concat(record.key) : expandedRowKeys.filter(function (o) {
          return o !== record.key;
        })
      });
    }
  }, {
    key: "columns",
    get: function get() {
      var _this$props = this.props,
          path = _this$props.path,
          _this$props$onShield = _this$props.onShield,
          onShield = _this$props$onShield === void 0 ? function (e) {
        return e;
      } : _this$props$onShield;
      return [{
        title: intl.get("ciam.roleManagement.model.roleManagement.permissionSetName").d('权限层级名称'),
        dataIndex: 'name'
      }, {
        title: intl.get("ciam.roleManagement.model.roleManagement.permission.Type").d('权限类型'),
        dataIndex: 'permissionType',
        width: 150,
        render: function render() {
          var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
          var record = arguments.length > 1 ? arguments[1] : undefined;
          var texts = {
            api: intl.get('ciam.roleManagement.view.message.api').d('API'),
            button: intl.get('ciam.roleManagement.view.message.button').d('按钮'),
            table: intl.get('ciam.roleManagement.view.message.table').d('表格列'),
            formItem: intl.get('ciam.roleManagement.view.message.formItem').d('表单项'),
            formField: intl.get('ciam.roleManagement.view.message.formField').d('表单域')
          };
          var valueList = value.split(',') || [];
          var text = valueList.map(function (item) {
            return texts[item] ? texts[item] : '';
          }) || [];
          return record.type === 'ps' && /*#__PURE__*/React.createElement(Tag, {
            color: value === 'api' ? 'green' : 'orange'
          }, text.join());
        }
      }, {
        dataIndex: 'shieldFlag',
        title: intl.get('ciam.roleManagement.model.roleManagement.isShield').d('是否屏蔽'),
        width: 90,
        render: yesOrNoRender
      }, {
        key: 'operator',
        title: intl.get('cpaas.common.button.action').d('操作'),
        width: 100,
        fixed: 'right',
        render: function render(_, _ref) {
          var type = _ref.type,
              secGrpAclId = _ref.secGrpAclId,
              shieldFlag = _ref.shieldFlag;

          if (type === 'ps') {
            var shieldBtn = [{
              key: 'shield',
              ele: /*#__PURE__*/React.createElement(ButtonPermission, {
                type: "text",
                permissionList: [{
                  code: "".concat(path, ".button.shield"),
                  type: 'button',
                  meaning: '角色管理-屏蔽访问权限'
                }],
                onClick: function onClick() {
                  return onShield({
                    shieldFlag: shieldFlag,
                    authorityId: secGrpAclId,
                    authorityType: 'ACL'
                  });
                }
              }, shieldFlag ? intl.get('ciam.roleManagement.view.button.cancelShield').d('取消屏蔽') : intl.get('ciam.roleManagement.view.button.shield').d('屏蔽')),
              len: shieldFlag ? 4 : 2,
              title: shieldFlag ? intl.get('ciam.roleManagement.view.button.cancelShield').d('取消屏蔽') : intl.get('ciam.roleManagement.view.button.shield').d('屏蔽')
            }];
            return operatorRender(shieldBtn);
          }
        }
      }];
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          loading = _this$props2.loading,
          dataSource = _this$props2.dataSource,
          _this$props2$shieldLo = _this$props2.shieldLoading,
          shieldLoading = _this$props2$shieldLo === void 0 ? false : _this$props2$shieldLo;
      var expandedRowKeys = this.state.expandedRowKeys;
      var tableProps = {
        rowKey: 'id',
        columns: this.columns,
        bordered: true,
        dataSource: dataSource,
        loading: loading || shieldLoading,
        childrenColumnName: 'subMenus',
        pagination: false,
        expandedRowKeys: expandedRowKeys,
        scroll: {
          x: tableScrollWidth(this.columns)
        },
        onExpand: this.onExpand
      };
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
        className: "action",
        style: {
          textAlign: 'right',
          marginBottom: '10px'
        }
      }, /*#__PURE__*/React.createElement(Button, {
        onClick: this.collapseAll,
        style: {
          marginRight: 8
        }
      }, intl.get("cpaas.common.button.collapseAll").d('全部收起')), /*#__PURE__*/React.createElement(Button, {
        onClick: this.expandAll
      }, intl.get("cpaas.common.button.expandAll").d('全部展开'))), /*#__PURE__*/React.createElement(Table, tableProps));
    }
  }]);

  return VisitPermissionTab;
}(Component), (_applyDecoratedDescriptor(_class.prototype, "expandAll", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "expandAll"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "collapseAll", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "collapseAll"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onExpand", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "onExpand"), _class.prototype)), _class));
export { VisitPermissionTab as default };